package code.Frame.adminFrame;

import code.Frame.FrameManager;
import code.MYSQL;
import code.Main;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.sql.SQLException;

public class CIinput {
    public JPanel panel;
    private JButton confirm;
    private JTextField name;
    private JTextField pos;
    private JLabel label1;
    private JLabel label2;

    private int status;
    private int cid;

    public boolean end = false;

    public CIinput(int s, int c)
    {
        status = s;
        cid = c;
        confirm.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                String Name = "'" + name.getText() + "'";
                String Pos = "'" + pos.getText() + "'";
                if (Name.length() == 2)
                {
                    JOptionPane.showMessageDialog(panel.getParent(), "食堂名不能为空");
                    return ;
                }
                if (Name.length() > 32)
                {
                    JOptionPane.showMessageDialog(panel.getParent(), "食堂名不能超过30位");
                    return ;
                }
                if (Pos.length() == 2)
                {
                    JOptionPane.showMessageDialog(panel.getParent(), "食堂位置不能为空");
                    return ;
                }
                if (Pos.length() > 102)
                {
                    JOptionPane.showMessageDialog(panel.getParent(), "食堂位置太长了");
                    return ;
                }
                try
                {
                    if (MYSQL.exist_canteen_name(Name))
                    {
                        JOptionPane.showMessageDialog(panel.getParent(), "食堂名字重复了");
                        return ;
                    }
                } catch (SQLException ex)
                {
                    throw new RuntimeException(ex);
                }
                if (status == 1) // 修改
                {
                    try
                    {
                        MYSQL.maintain("update canteen set " +
                                    "name = " + Name + ", " +
                                    "pos = " + Pos + "\n" +
                                    "where CID = " + cid
                                );
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
                else // 新增
                {
                    try
                    {
                        MYSQL.maintain("insert into canteen(name, pos, UID) values( " +
                                Name + ", " +
                                Pos + ", " +
                                (Main.uid - 300000000) + ")"
                        );
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
                FrameManager.ciFrame.refresh_table();
                end = true;
            }
        });
    }
}
